define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'template', 'echarts', 'echarts-theme'], function ($, undefined, Backend, Table, Form, Template, Echarts) {

    var Controller = {
        index: function () {
            //这句话在多选项卡统计表时必须存在，否则会导致影响的图表宽度不正确
            $(document).on("click", ".charts-custom a[data-toggle=\"tab\"]", function () {
                var that = this;
                setTimeout(function () {
                    var id = $(that).attr("href");
                    var chart = Echarts.getInstanceByDom($(id)[0]);
                    chart.resize();
                }, 0);
            });
            //初始加载
            ajax_statistics();
            ajax_sales();
            ajax_ranking();

            $("#statistics").click(function(){
                ajax_statistics();
            });
            $("#sales").click(function(){
                ajax_sales();
            });
            $("#ranking").click(function(){
                ajax_ranking();
            });
            $("ul#selectdate").on("click","li",function(){
                var times = $(this).data("times");
                $('#example_times').val(times);

                ajax_statistics();
                ajax_sales();
                ajax_ranking();
            });

            function ajax_statistics() {
                var example_times = $('#example_times').val();
                var time_statistics = $("#time_statistics").val();
                var admin_id = $("#c-admin_id option:selected").val();
                Fast.api.ajax({
                    url: "feditionchart/ajax_statistics",
                    async: false,
                    data: {example_times: example_times,time: time_statistics,admin_id: admin_id}
                }, function (data) {
                    if(data.single_quantityz == null){
                        data.single_quantityz = 0;
                    }
                    if(data.salesz == null){
                        data.salesz = 0;
                    }
                    if(data.advertisingz == null){
                        data.advertisingz = 0;
                    }
                    if(data.roi == null){
                        data.roi = 0;
                    }
                    $('#ajax_single_quantity').html(data.single_quantityz);
                    $('#ajax_sales').html(data.salesz);
                    $('#ajax_advertising').html(data.advertisingz);
                    $('#ajax_amount').html(data.roi);
                    // $('#time_statistics_usa').html(data.usatimes);
                    return false;
                });
            }
            function ajax_sales() {
                var example_times = $('#example_times').val();
                var time_sales = $("#time_sales").val();
                var admin_id = $("#c-admin_id2 option:selected").val();
                Fast.api.ajax({
                    url: "feditionchart/ajax_sales",
                    data: {example_times: example_times,time: time_sales,admin_id: admin_id}
                }, function (ajax_sales_data) {
                    // 基于准备好的dom，初始化echarts实例
                    var lineChart = Echarts.init(document.getElementById('line-chart'), 'walden');
                    // 指定图表的配置项和数据
                    var option = {
                        legend: {
                            data: ajax_sales_data.arrtitle
                        },
                        label: {},
                        tooltip: {
                            trigger: 'axis'   // axis   item   none三个值
                        },
                        xAxis: {
                            type: 'category',
                            data: ajax_sales_data.arrtimedate,
                            name: '日期',   // x轴名称
                            // x轴名称样式
                            nameTextStyle: {
                                fontWeight: 600,
                                fontSize: 18
                            }
                        },
                        yAxis: {
                            type: 'value',
                            name: '金額',   // y轴名称
                            // y轴名称样式
                            nameTextStyle: {
                                fontWeight: 600,
                                fontSize: 18
                            }
                        },
                        series: [
                            {
                                name: ajax_sales_data.arrtitle[0],
                                data: ajax_sales_data.arrsalesz,
                                type: 'line'
                            },{
                                name: ajax_sales_data.arrtitle[1],
                                data: ajax_sales_data.arradvertisingz,
                                type: 'line'
                            },{
                                name: ajax_sales_data.arrtitle[2],
                                data: ajax_sales_data.arrsingle_quantityz,
                                type: 'line'
                            }
                        ]
                    };
                    // 使用刚指定的配置项和数据显示图表。
                    lineChart.setOption(option);
                    return false;
                });
            }
            function ajax_ranking() {
                var example_times = $('#example_times').val();
                var time_ranking = $("#time_ranking").val();
                Fast.api.ajax({
                    url: "feditionchart/ajax_ranking",
                    data: {example_times: example_times,time: time_ranking}
                }, function (ajax_ranking_data) {
                    // 基于准备好的dom，初始化echarts实例
                    //柱状图
                    var barChart = Echarts.init(document.getElementById('bar-chart'), 'walden');
                    option = {
                        tooltip: {
                            trigger: 'axis',
                            axisPointer: {            // Use axis to trigger tooltip
                                type: 'shadow'        // 'shadow' as default; can also be 'line' or 'shadow'
                            }
                        },
                        legend: {
                            data: ajax_ranking_data.name
                        },
                        grid: {
                            left: '3%',
                            right: '4%',
                            bottom: '3%',
                            containLabel: true
                        },
                        xAxis: {
                            type: 'value'
                        },
                        yAxis: {
                            type: 'category',
                            data: ajax_ranking_data.date
                        },
                        series: [
                            {
                                name: ajax_ranking_data.name[0],
                                type: 'bar',
                                stack: 'total',
                                label: {
                                    show: true
                                },
                                emphasis: {
                                    focus: 'series'
                                },
                                data: ajax_ranking_data.newadmin[0]
                            },
                            {
                                name: ajax_ranking_data.name[1],
                                type: 'bar',
                                stack: 'total',
                                label: {
                                    show: true
                                },
                                emphasis: {
                                    focus: 'series'
                                },
                                data: ajax_ranking_data.newadmin[1]
                            },
                            {
                                name: ajax_ranking_data.name[2],
                                type: 'bar',
                                stack: 'total',
                                label: {
                                    show: true
                                },
                                emphasis: {
                                    focus: 'series'
                                },
                                data: ajax_ranking_data.newadmin[2]
                            },
                            {
                                name: ajax_ranking_data.name[3],
                                type: 'bar',
                                stack: 'total',
                                label: {
                                    show: true
                                },
                                emphasis: {
                                    focus: 'series'
                                },
                                data: ajax_ranking_data.newadmin[3]
                            }
                        ]
                    };
                    // 使用刚指定的配置项和数据显示图表。
                    barChart.setOption(option);
                    return false;
                });
            }
        }
    };
    return Controller;
});

